#ifndef LOG_BUFFER_H
#define LOG_BUFFER_H

#include <cstring>
#include <string>

namespace Logger
{
    template <int SIZE>
    class LogBuffer
    {
    public:
        static constexpr int sSmallBufferSize = 4000;
        static constexpr int sLargeBufferSize = 4000 * 1000;

        LogBuffer() : size_(0), cur_(data_)
        {
        }

        size_t avail() const
        {
            return static_cast<size_t>(SIZE - this->size_);
        }

        void append(const char *buf, size_t len)
        {
            if (this->avail() > len)
            {
                ::memcpy(this->cur_, buf, len);
                this->add(len);
            }
        }

        void add(size_t len)
        {
            this->cur_ += len;
            this->size_ += len;
        }

        void clear()
        {
            ::bzero(this->data_, SIZE);
        }

        void reset()
        {
            this->cur_ = data_;
            this->size_ = 0;
        }

        // std::string toString() const
        // {
        //     return std::string(this->data_, this->size_);
        // }

        const char *getData() const { return this->data_; } // const char *：返回的指针不可被修改

        int getLen() const { return this->size_; }

        char *getCur() const { return this->cur_; } // char *：LogStream的LogBuffer不是常量，需要写入

    private:
        char data_[SIZE]; // 静态数组不需要在构造函数初始化列表中初始化
        int size_;        // 有效数据长度
        char *cur_;       // 下一个可写入位置
    };
} // namespace Logger

#endif // LOG_BUFFER_H